package com.e2oto.seller.modules.launch.presenter;


import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.common.EasyAES;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.base.BasePresenter;
import com.e2oto.seller.common.http.ProgressSubscriber;
import com.e2oto.seller.common.http.ProgressSubscriberOnNextListener;
import com.e2oto.seller.common.utils.RetrofitUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.launch.bean.CodeBean;
import com.e2oto.seller.modules.launch.bean.LoginBean;
import com.e2oto.seller.modules.launch.views.RegisterView;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;


/**
 * Created by ZhangLiQiang on 2017/8/5.
 */

public class RegisterPresenter extends BasePresenter<RegisterView> {


    /***
     * 发验证码
     * @param phone
     */
    public void sendCode(String phone) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile", phone);
        String param = EasyAES.encrypt(jsonObject);

        final MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)//表单类型
                .addFormDataPart("ver", SellerApp.versionName)
                .addFormDataPart("param", param);

        Observable<BaseBean<CodeBean>> observable = RetrofitUtils.getApiService().sendRegCode(builder.build());
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean<CodeBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseBean<CodeBean> baseBean) {
                        if (baseBean.getCode() == 200) {
                            getView().sendCodeCallback(baseBean.getData());
                        } else {
                            getView().showMessage(baseBean.getMsg() + "");
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /***
     * 常规登录
     * @param phone
     * @param password
     */
    public void register(String phone, String code, String password) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile", phone);
        jsonObject.put("pwd", password);
        jsonObject.put("code", code);
        String param = EasyAES.encrypt(jsonObject);

        ProgressSubscriber<LoginBean> progressSubscriber = new ProgressSubscriber<>(new ProgressSubscriberOnNextListener<LoginBean>() {

            @Override
            public void onNext(LoginBean bean) {
                getView().registerCallback(bean);
            }
        }, getContext());
        Observable<BaseBean<LoginBean>> observable = RetrofitUtils.getApiService().register(param, SellerApp.versionName);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(progressSubscriber);

    }
}
